package com.sh.suhe.utildemos.views.activity.photo.util;

import android.text.TextUtils;

import com.sh.suhe.utildemos.model.PaPhotoModel;
import com.sh.suhe.utildemos.model.PaPhotoTypeModel;
import com.sh.suhe.utildemos.utils.JsonUtil;
import com.sh.suhe.utildemos.utils.LogUtil;
import com.sh.suhe.utildemos.utils.http.NetUtil;
import com.sh.suhe.utildemos.views.activity.photo.PaPhotoActivity;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MeiTuUtils {
    private static String TAG = MeiTuUtils.class.getSimpleName();
    public static String url = "https://sc.chinaz.com/tupian/";

    public static void loadUrl(String url, PaPhotoActivity.LoadPhoto loadPhoto) {
        LogUtil.e(TAG, url);
        NetUtil.request(NetUtil.RequestMethod.GET, url, new HashMap<>(), new NetUtil.RequestCallBack() {
            @Override
            public void onSuccess(int statusCode, String json) {
                List<PaPhotoTypeModel> types = new ArrayList<>();
                List<PaPhotoModel> imgList = new ArrayList<>();
                //mt10 feilei
                Document doc = Jsoup.parse(json);
                //获取所有的博客列表集合
                Elements blogList = doc.select("div[class=feilei_a]");
                for (Element e: blogList){
                    Elements imgEle =  e.select("a");
                    for (Element a : imgEle) {
                        String type = a.text();
                        String href = a.attr("href");
                        PaPhotoTypeModel model = new PaPhotoTypeModel();
                        model.setType(type);
                        model.setUrl("https://sc.chinaz.com"+href);
                        types.add(model);
                    }
                }
                Elements imgs = doc.select("div[class=pic_wrap]");
                if (imgs == null || imgs.size() == 0){
                    imgs = doc.select("div[id=container]");
                }
                List<String> imgUrls = new ArrayList<>();
                List<String> imgDetailUrls = new ArrayList<>();
                for (Element e: imgs){
                    Elements imgEle =  e.select("img");

                    for (Element img : imgEle){
                        String path = img.attr("src");
                        if (TextUtils.isEmpty(path)){
                            path = img.attr("src2");
                        }

                        imgUrls.add("https:" + path);
                    }
                    Elements aEle =  e.select("a");
                    for (int i=0;i< aEle.size() ; i++ ){
                        if (i % 2 == 0) {
                            Element a = aEle.get(i);
                            String path = a.attr("href");
                            if (TextUtils.isEmpty(path)) {
                                path = a.attr("src2");
                            }

                            imgDetailUrls.add("https:" + path);
                        }
                    }
                    for (int i = 0; i< imgUrls.size(); i++) {
                        PaPhotoModel model = new PaPhotoModel();
                        model.setImg( imgUrls.get(i));
                        if (i < imgDetailUrls.size()) {
                            model.setDetailUrl( imgDetailUrls.get(i));
                        }
                        imgList.add(model);
                    }
                }
                Elements next = doc.select("a[class=nextpage]");
                String nextPage = "";
                for (Element n: next){
                    nextPage = " https://sc.chinaz.com/tupian/"+ n.attr("href");
                }

//                LogUtil.e(TAG,imgList.size()+ JsonUtil.object2Json(imgList));
                loadPhoto.loadBack(imgList,types, nextPage);
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                LogUtil.e(TAG,errorMsg);
            }
        });
    }
}
